import cv2
image_path = ‘C:/Users/Lenovo/OneDrive/photo.jpg’ # 替换为你的图片路径
image = cv2.imread(image_path)
if image is None:
print(“无法读取图片，请检查路径。”)
else:
# 2. 转换为灰度图
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# 3. 显示原图和灰度图
cv2.imshow('Original Image', image)
cv2.imshow('Gray Image', gray_image)

# 等待按键，然后关闭所有窗口
cv2.waitKey(0)
cv2.destroyAllWindows()

# 4. 可选：保存灰度图
cv2.imwrite('C:/Users/Lenovo/OneDrive/photo.jpg', gray_image)  # 替换为保存路径